/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * WelcomePage.java
 *
 * Created on Jan 9, 2013, 2:30:55 PM
 */
package gui;

import engine.Main;
import java.awt.Color;
import java.awt.Component;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;

/**
 *
 * @author Lydia
 */
public class WelcomePage extends javax.swing.JFrame {

    /** Creates new form WelcomePage */
    public WelcomePage() {
        initComponents();
        this.setTitle("Welcome Page");
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        codeArea = new javax.swing.JTextArea();
        run = new javax.swing.JButton();
        trace = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        error = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        supportedInst = new javax.swing.JButton();
        mangReg = new javax.swing.JButton();
        ManageMem = new javax.swing.JButton();
        editSettings = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Toma Sim");
        setBounds(new java.awt.Rectangle(300, 100, 0, 0));

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Program", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Agency FB", 1, 18), new java.awt.Color(0, 153, 153))); // NOI18N

        codeArea.setColumns(20);
        codeArea.setRows(5);
        jScrollPane1.setViewportView(codeArea);

        run.setText("Run");
        run.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                runActionPerformed(evt);
            }
        });

        trace.setText("Trace");
        trace.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                traceActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 227, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(run, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(trace, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(run)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(trace))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 379, Short.MAX_VALUE))
                .addContainerGap())
        );

        jLabel1.setFont(new java.awt.Font("Copperplate Gothic Bold", 1, 18)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(79, 92, 146));
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("Welcome to Toma-Sim !!!");
        jLabel1.setBorder(javax.swing.BorderFactory.createCompoundBorder(null, javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0))));

        error.setFont(new java.awt.Font("Tahoma", 2, 11)); // NOI18N
        error.setForeground(new java.awt.Color(255, 51, 51));
        error.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        error.setText(" ");

        jPanel2.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        supportedInst.setText("Supported Instructions");
        supportedInst.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                supportedInstActionPerformed(evt);
            }
        });

        mangReg.setText("Manage Registers");
        mangReg.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mangRegActionPerformed(evt);
            }
        });

        ManageMem.setText("Manage Memory");
        ManageMem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ManageMemActionPerformed(evt);
            }
        });

        editSettings.setText("Edit Settings");
        editSettings.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                editSettingsActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(ManageMem, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(mangReg, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(supportedInst, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addComponent(editSettings, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 145, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(supportedInst)
                .addGap(18, 18, 18)
                .addComponent(mangReg)
                .addGap(18, 18, 18)
                .addComponent(ManageMem)
                .addGap(18, 18, 18)
                .addComponent(editSettings)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(error, javax.swing.GroupLayout.DEFAULT_SIZE, 224, Short.MAX_VALUE))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(38, 38, 38)
                                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(114, 114, 114)
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 342, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(15, 15, 15)
                        .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(118, 118, 118)
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(31, 31, 31)
                        .addComponent(error)))
                .addGap(24, 24, 24))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void supportedInstActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_supportedInstActionPerformed
        // TODO add your handling code here:
        GUIMain.i.setVisible(true);
    }//GEN-LAST:event_supportedInstActionPerformed

    String file="Prog.in";
    private void runActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_runActionPerformed
        // TODO add your handling code here:
        try {
            BufferedWriter out = new BufferedWriter(new FileWriter(new File(file)));
            out.write(codeArea.getText() + "\n");
            out.close();
        } catch (IOException ex) {
            Logger.getLogger(WelcomePage.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        if (GUIMain.isSettings) {
            try {
                GUIMain.m.loadCode();
            } catch (IOException ex) {
                Logger.getLogger(WelcomePage.class.getName()).log(Level.SEVERE, null, ex);
            }
            GUIMain.m.runAll();
            GUIMain.f = new FinalPage();
            GUIMain.f.setVisible(true);
            GUIMain.w.setVisible(false);
        } else {
            error.setText("Please Edit Your Preferred Settings First");
        }
        
    }//GEN-LAST:event_runActionPerformed

    private void editSettingsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_editSettingsActionPerformed
        // TODO add your handling code here:
        GUIMain.s.setVisible(true);
        GUIMain.w.setVisible(false);
    }//GEN-LAST:event_editSettingsActionPerformed

    private void traceActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_traceActionPerformed
        // TODO add your handling code here:
        try {
            BufferedWriter out = new BufferedWriter(new FileWriter(new File(file)));
            out.write(codeArea.getText() + "\n");
            out.close();
        } catch (IOException ex) {
            Logger.getLogger(WelcomePage.class.getName()).log(Level.SEVERE, null, ex);
        }
        if (GUIMain.isSettings) {
            try {
                GUIMain.m.loadCode();
            } catch (IOException ex) {
                Logger.getLogger(WelcomePage.class.getName()).log(Level.SEVERE, null, ex);
            }
        GUIMain.c = new Contents();
        
        //Data Memory
        DefaultTableModel model = (DefaultTableModel) GUIMain.c.jTable9.getModel();
        for (int j=0; j<GUIMain.m.Datam.memory.size();j++) {
            Object[] data = {new Object(), new Object()};
            model.addRow(data);
            GUIMain.c.jTable9.getModel().setValueAt(j, j, 0);
            GUIMain.c.jTable9.getModel().setValueAt(GUIMain.m.Datam.memory.get(j), j, 1);
        }
        
        Color blue = Color.BLUE;
        Color green = Color.GREEN;
        Color cur;
        
        //L1 data cache
        GUIMain.c.jPanel4.setVisible(false);
        GUIMain.c.jPanel5.setVisible(false);
        DefaultTableModel model2 = (DefaultTableModel) GUIMain.c.jTable6.getModel();
        for (int j=0;j<GUIMain.m.dc1L;j++) {
            model2.addColumn("Word " + (j+1));
        }
        for (int i = 0; i < GUIMain.m.dc1S/(GUIMain.m.dc1L*GUIMain.m.dc1M); i++) {
            if (i%2 == 0) {
                cur = blue;
            } else {
                cur = green;
            }
            for (int j = 0; j < GUIMain.m.dc1M; j++) {
                Object[] data = new Object[GUIMain.m.dc1L];
                model2.addRow(data);
            }
            Object[] data = new Object[GUIMain.m.dc1L];
            model2.addRow(data);
        }
        if (GUIMain.m.noDataCaches == 2 || GUIMain.m.noDataCaches == 3) {
            //L2 data cache
            GUIMain.c.jPanel4.setVisible(true);
            GUIMain.c.jPanel5.setVisible(false);
            DefaultTableModel model3 = (DefaultTableModel) GUIMain.c.jTable7.getModel();
            for (int j=0;j<GUIMain.m.dc2L;j++) {
                model3.addColumn("Word " + (j+1));
            }
            for (int i = 0; i < GUIMain.m.dc2S/(GUIMain.m.dc2L*GUIMain.m.dc2M); i++) {
                for (int j = 0; j < GUIMain.m.dc2M; j++) {
                    Object[] data = new Object[GUIMain.m.dc2L];
                    model3.addRow(data);
                }
                Object[] data = new Object[GUIMain.m.dc2L];
                model3.addRow(data);
            }
        }
        if (GUIMain.m.noDataCaches == 3) {
            //L3 data cache
            GUIMain.c.jPanel4.setVisible(true);
            GUIMain.c.jPanel5.setVisible(true);
            DefaultTableModel model4 = (DefaultTableModel) GUIMain.c.jTable8.getModel();
            for (int j=0;j<GUIMain.m.dc3L;j++) {
                model4.addColumn("Word " + (j+1));
            }
            for (int i = 0; i < GUIMain.m.dc3S/(GUIMain.m.dc3L*GUIMain.m.dc3M); i++) {
                for (int j = 0; j < GUIMain.m.dc3M; j++) {
                    Object[] data = new Object[GUIMain.m.dc3L];
                    model4.addRow(data);
                }
                Object[] data = new Object[GUIMain.m.dc2L];
                model4.addRow(data);
            }
        }
        
        //ROB
        DefaultTableModel model5 = (DefaultTableModel) GUIMain.c.jTable1.getModel();
        for (int i=0; i<GUIMain.m.ROBEntries;i++) {
            Object[] data = new Object[6];
            model5.addRow(data);
        }
        
        //RSs
        DefaultTableModel model6 = (DefaultTableModel) GUIMain.c.jTable2.getModel();
        for (int i=0; i<GUIMain.m.n;i++) {
            Object[] data = new Object[9];
            model6.addRow(data);
        }
        
        //Regsiters
        GUIMain.c.jLabel1.setText("R0: " + GUIMain.m.register.read("R0"));
        GUIMain.c.jLabel2.setText("R1: " + GUIMain.m.register.read("R1"));
        GUIMain.c.jLabel3.setText("R2: " + GUIMain.m.register.read("R2"));
        GUIMain.c.jLabel4.setText("R3: " + GUIMain.m.register.read("R3"));
        GUIMain.c.jLabel5.setText("R4: " + GUIMain.m.register.read("R4"));
        GUIMain.c.jLabel6.setText("R5: " + GUIMain.m.register.read("R5"));
        GUIMain.c.jLabel7.setText("R6: " + GUIMain.m.register.read("R6"));
        GUIMain.c.jLabel8.setText("R7: " + GUIMain.m.register.read("R7"));
        
        try {
            GUIMain.m.loadCode();
        } catch (IOException ex) {
            Logger.getLogger(WelcomePage.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        GUIMain.c.setVisible(true);
        GUIMain.w.setVisible(false);}
        else {
            error.setText("Please Edit Your Preferred Settings First");
        }
    }//GEN-LAST:event_traceActionPerformed

    private void ManageMemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ManageMemActionPerformed
        // TODO add your handling code here:
        if (GUIMain.isSettings) {
            GUIMain.mg.setVisible(true);
        }
        else {
            error.setText("Please Edit Your Preferred Settings First");
        }
    }//GEN-LAST:event_ManageMemActionPerformed

    private void mangRegActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mangRegActionPerformed
        // TODO add your handling code here:
        if (GUIMain.isSettings) {
            GUIMain.rg.setVisible(true);
        }
        else {
            error.setText("Please Edit Your Preferred Settings First");
        }
    }//GEN-LAST:event_mangRegActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new WelcomePage().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton ManageMem;
    private javax.swing.JTextArea codeArea;
    private javax.swing.JButton editSettings;
    private javax.swing.JLabel error;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton mangReg;
    private javax.swing.JButton run;
    private javax.swing.JButton supportedInst;
    private javax.swing.JButton trace;
    // End of variables declaration//GEN-END:variables
}